<?php

class Bs_boot {
	public function __construct() {
	   add_action( 'admin_enqueue_scripts', array(&$this, 'bs_admin_enqueue_scripts') );
	   add_action( 'init', array(&$this, 'create_boot_post_type') );
	   add_filter( 'manage_boots_posts_columns', array(&$this, 'replace_boot_columns'), 100 );
	   add_action( 'manage_boots_posts_custom_column', array(&$this, 'add_boot_columns_data') );
	   add_filter( 'manage_edit-boots_sortable_columns', array(&$this, 'sortable_columns') );
	   //add_action( 'pre_get_posts', array(&$this, 'columns_orderby') );
	   add_action( 'restrict_manage_posts', array(&$this,'restrict_listings_by_brand') );
	   add_filter( 'parse_query', array(&$this, 'convert_brand_id_to_taxonomy_term_in_query') );
	   add_action( 'init', array(&$this,'boots_taxanomy_init' ) );
	   add_action( 'admin_head', array(&$this, 'remove_seo_filter'), 20 );
	}
	
	/**
	 * Add stylesheets and scripts only for admin
	 */
	public function bs_admin_enqueue_scripts($hook)
	{
	   wp_register_style( 'bs-admin-css', BS_ASSETS_URL . 'css/admin.css', false, '1.0.0' );
	   wp_enqueue_style( 'bs-admin-css' );
	   
	   wp_enqueue_script( 'bs-admin-js', BS_ASSETS_URL. 'js/admin.js', array('jquery'), '1.0.0' );
	}

	public function create_boot_post_type() {
		
		register_post_type('boots',
			array(
				'labels' => array(
					'name' => __( 'Støvler' ),
					'singular_name' => __( 'Støvle' )
				),
				'public' => true,
      			'has_archive' => true,
				'rewrite' => array('slug' => 'fodboldstoevler'),
      			'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
			)
		);
	}

	public function replace_boot_columns($columns) {

		$new_columns = array(
			'brand' => __('Mærke', 'fodfessor'),
			'level' => __('Niveau', 'fodfessor'),
			'material' => __('Materiale', 'fodfessor'),
			'foot_type' => __('Fodtype', 'fodfessor'),
			'surface' => __('Underlag', 'fodfessor'),
			'player_type' => __('Spillertype', 'fodfessor')
		);
		return array_merge(array_slice($columns, 0, 3), $new_columns);
	}

	public function add_boot_columns_data($column) {

		switch ($column) {
			case 'thumbnail':
				$value = get_post_meta(get_the_ID());
				echo $value[$column][0];
				break;

			case 'brand':
				$value = wp_get_post_terms(get_the_ID(), $column);
				echo $value[0]->name;
				break;

			case 'level':
				$value = wp_get_post_terms(get_the_ID(), $column);
				echo $value[0]->name;
				break;

			case 'material':
				$values = wp_get_post_terms(get_the_ID(), $column);
				foreach($values as $i=>$value) {
					echo $values[$i]->name . "\n";
				}
				break;

			case 'foot_type':
				$value = wp_get_post_terms(get_the_ID(), $column);
				echo $value[0]->name;
				break;

			case 'surface':
				$value = wp_get_post_terms(get_the_ID(), $column);
				echo $value[0]->name;
				break;

			case 'player_type':
				$value = wp_get_post_terms(get_the_ID(), $column);
				echo $value[0]->name;
				break;
		}
	}

	public function sortable_columns( $columns ) {
		$columns['brand'] = 'brand';
	    $columns['level'] = 'level';
	    $columns['material'] = 'material';
	    $columns['foot_type'] = 'foot_type';
	    $columns['surface'] = 'surface';
	    $columns['player_type'] = 'player_type';
	 
	    return $columns;
	}

	public function restrict_listings_by_brand() {
	    global $typenow;
	    global $wp_query;
	    if ($typenow=='boots') {
	       $taxonomy_brand = 'brand';
	       $taxonomy_level = 'level';
	       $taxonomy_foot = 'foot_type';
	        //$region_taxonomy = get_taxonomy($taxonomy);var_dump($region_taxonomy);
			$term = isset($wp_query->query['brand']) ? $wp_query->query['brand'] :'';
	        wp_dropdown_categories(array(
	            'show_option_all' =>  __("Vis alle mærker"),
	            'taxonomy'        =>  $taxonomy_brand,
	            'name'            =>  'brand',
	            'orderby'         =>  'name',
	            'selected'        =>  $term,
	            'hierarchical'    =>  true,
	            'depth'           =>  3,
	            'show_count'      =>  true, // Show # listings in parens
	            'hide_empty'      =>  false // Don't show businesses w/o listings
	        ));
	        $term = isset($wp_query->query['level']) ? $wp_query->query['level'] :'';
	        wp_dropdown_categories(array(
	            'show_option_all' =>  __("Vis alle niveauer"),
	            'taxonomy'        =>  $taxonomy_level,
	            'name'            =>  'level',
	            'orderby'         =>  'name',
	            'selected'        =>  $term,
	            'hierarchical'    =>  true,
	            'depth'           =>  3,
	            'show_count'      =>  true, // Show # listings in parens
	            'hide_empty'      =>  false // Don't show businesses w/o listings
	        ));
	        wp_dropdown_categories(array(
	            'show_option_all' =>  __("Vis alle fodtyper"),
	            'taxonomy'        =>  $taxonomy_foot,
	            'name'            =>  'foot_type',
	            'orderby'         =>  'name',
	            'selected'        =>  $term,
	            'hierarchical'    =>  true,
	            'depth'           =>  3,
	            'show_count'      =>  true, // Show # listings in parens
	            'hide_empty'      =>  false // Don't show businesses w/o listings
	        ));
	    }
	}

	public function convert_brand_id_to_taxonomy_term_in_query($query) {
	    global $pagenow;
	    $qv = &$query->query_vars;
	    if ($pagenow=='edit.php' && isset($qv['brand']) && is_numeric($qv['brand'])) {
	        $term = get_term_by('id',$qv['brand'],'brand');
	        $qv['brand'] = ($term ? $term->slug : '');
	    }
	    if ($pagenow=='edit.php' && isset($qv['level']) && is_numeric($qv['level'])) {
	        $term = get_term_by('id',$qv['level'],'level');
	        $qv['level'] = ($term ? $term->slug : '');
	    }
	    if ($pagenow=='edit.php' && isset($qv['foot_type']) && is_numeric($qv['foot_type'])) {
	        $term = get_term_by('id',$qv['foot_type'],'foot_type');
	        $qv['foot_type'] = ($term ? $term->slug : '');
	    }
	}

	public function remove_seo_filter() {
	    global $wpseo_metabox;
	    global $post;
	    $post_id = get_post_type($post->id);

	    if($post_id === 'boots') {

		    if ( $wpseo_metabox ) {
		        remove_action( 'restrict_manage_posts', array( $wpseo_metabox, 'posts_filter_dropdown' ) );
		    }
		}
	}

	public function boots_taxanomy_init() {
	// create a new taxonomy
		register_taxonomy(
			'brand',
			'boots',
			array(
				'label' => __( "Mærker" ),
				'rewrite' => array( 'slug' => 'fodboldstovler/maerke' ),
				'hierarchical' => true
			)
		);
		register_taxonomy(
			'level',
			'boots',
			array(
				'label' => __("Niveauer"),
				'rewrite' => array( 'slug' => 'level' ),
				'hierarchical' => true
			)
		);
		register_taxonomy(
			'material',
			'boots',
			array(
				'label' => __("Materialer"),
				'rewrite' => array( 'slug' => 'material' ),
				'hierarchical' => false
			)
		);
		register_taxonomy(
			'foot_type',
			'boots',
			array(
				'label' => __("Fodtyper"),
				'rewrite' => array( 'slug' => 'foot_type' ),
				'hierarchical' => true
			)
		);
		register_taxonomy(
			'surface',
			'boots',
			array(
				'label' => __("Underlag"),
				'rewrite' => array( 'slug' => 'surface' ),
				'hierarchical' => true
			)
		);
		register_taxonomy(
			'color',
			'boots',
			array(
				'label' => __("Farver"),
				'rewrite' => array( 'slug' => 'color' ),
				'hierarchical' => false
			)
		);
		register_taxonomy(
			'player_type',
			'boots',
			array(
				'label' => __("Spillertyper"),
				'rewrite' => array( 'slug' => 'player_type' ),
				'hierarchical' => true
			)
		);
	}
}

new Bs_boot(); 